import UserService.User;
import UserService.UserDaoImp;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet(name="RegisterServlet",value = "/RegisterServlet")
public class RegisterServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        req.setCharacterEncoding("UTF-8");
        String username = req.getParameter("username");//获取用户名
        String password = req.getParameter("password");//密码
        String apassword = req.getParameter("apassword");//重复密码
        String phone = req.getParameter("phone");//手机号
        String registerButton = req.getParameter("registerButton");//注册按钮
        String returnButton = req.getParameter("returnButton");//返回按钮

        String error = "    ";

        //用户按下注册按钮
        if (registerButton != null) {
            if (username == null || username.isEmpty() || password == null
                    || password.isEmpty()) {//判断用户名是否为空
                error = "账号或密码为空!请输入!";//赋值给他,然后传给前端
                req.setAttribute("error", error);//
                req.getRequestDispatcher("Register.jsp").forward(req, resp);//重定向
            } else if (password.equals(apassword)) {

                System.out.println(username);
                System.out.println(password);
                System.out.println(phone);
                UserDaoImp u = new UserDaoImp();//创建个类
                try {
                    if (u.finduser(username).getUname()==null) {//判断数据库能否查询到,查询不到就尚未注册,让他实现注册
                        u.createNewUser(username, password, phone);//创建方法进行创建
                        System.out.println("插入成功");
                        User user=new User(username,password,phone);
                        HttpSession session = req.getSession(true);
                        session.setMaxInactiveInterval(60 * 60);
                        session.setAttribute("user",user);

                        req.getRequestDispatcher("userSite.jsp").forward(req, resp);

                    }
                } catch (Exception e) {
                    error = "用户名已存在!";
                    req.setAttribute("error", error);
                    req.getRequestDispatcher("Register.jsp").forward(req, resp);
                }
            }

         else if (!password.equals(apassword)) {
            error = " 俩次密码输入不一致!";
            req.setAttribute("error", error);
            req.getRequestDispatcher("Register.jsp").forward(req, resp);
        }


        }else if(returnButton!=null){
            //用户按下返回按钮
            resp.sendRedirect("UserLogin.jsp");//返回用户登录界面

        }


    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }
}
